/**
 * 
 */
package com.gr.menuw.horizontal.dropdown.custom;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.resources.JavascriptResourceReference;
import org.apache.wicket.markup.repeater.RepeatingView;

import com.gr.menuw.horizontal.dropdown.custom.css.CustomMenuCssScope;
import com.gr.menuw.horizontal.dropdown.custom.js.CustomMenuJsScope;
import com.gr.menuw.menu.IMenu;
import com.gr.menuw.menu.item.IMenuItem;


/**
 * @author Graham Rhodes 25 Feb 2011 21:48:04
 */
public class CustomMenu extends Panel implements IMenu, IHeaderContributor {

    private static final long serialVersionUID = 1L;
    private static final JavascriptResourceReference JS = new JavascriptResourceReference(CustomMenuJsScope.class, "menu.js");
    protected static final ResourceReference CSS = new ResourceReference(CustomMenuCssScope.class, "menu.css");
    private final List<IMenuItem> items = new ArrayList<IMenuItem>();
    private final WebMarkupContainer toolbar;
    private final RepeatingView itemsView;

    public CustomMenu(final String id) {
        super(id);

        setRenderBodyOnly(true);

        toolbar = new WebMarkupContainer("toolbar") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(final ComponentTag tag) {
                super.onComponentTag(tag);
                tag.getAttributes().put("class", tag.getAttributes().get("class") + " " + id);
            }
        };

        toolbar.setOutputMarkupId(false);

        add(toolbar);

        itemsView = new RepeatingView("items");
        toolbar.addOrReplace(itemsView);
    }

    /* (non-Javadoc)
     * @see org.apache.wicket.markup.html.IHeaderContributor#renderHead(org.apache.wicket.markup.html.IHeaderResponse)
     */
    @Override
    public void renderHead(IHeaderResponse response) {
        response.renderCSSReference(CSS);
        response.renderJavascriptReference(JS);
    }

    @Override
    protected void onBeforeRender() {

        itemsView.removeAll();

        for (IMenuItem item : this.items) {
            if (item instanceof Component) {
                itemsView.add((Component) item);
            }
        }

        super.onBeforeRender();
    }

    /* (non-Javadoc)
     * @see com.gr.menuw.menu.IMenu#isOnTop()
     */
    @Override
    public boolean isOnTop() {
        return true;
    }

    /* (non-Javadoc)
     * @see com.gr.menuw.menu.IMenu#addItem(com.gr.menuw.menu.item.IMenuItem)
     */
    @Override
    public IMenu addItem(IMenuItem item) {
        items.add(item);
        return this;
    }

    /* (non-Javadoc)
     * @see com.gr.menuw.menu.IMenu#removeItem(com.gr.menuw.menu.item.IMenuItem)
     */
    @Override
    public IMenu removeItem(IMenuItem item) {
        items.add(item);
        return this;
    }

    public void hideItem(IMenuItem menuItem) {
        items.get(items.indexOf(menuItem)).show(false);
    }

    public void showItem(IMenuItem menuItem) {
        items.get(items.indexOf(menuItem)).show(true);
    }

}
